<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shopkeeper extends CI_Controller {

    private function get_category_list_data($with_null)
    {
        $shop_id = $this->session->userdata('shop_id');
        $userid = $this->session->userdata('userid');

        $this->load->model('Category_model');
        $all = $this->Category_model->get_all($shop_id, $userid);

        return get_list_data($all, 'Id', 'Name', $with_null);
    }

    function get_category()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $with_null = trim($this->input->get_post('with_null'));

        $data = $this->get_category_list_data($with_null);

        hint_message(true, '', array('items' => $data));
    }

    function get_system_category()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $with_null = trim($this->input->get_post('with_null'));
        $parent_id = trim($this->input->get_post('parent_id'));
        $level = intval($this->input->get_post('level'));

        if ($level > 1 and $parent_id === '') {
            return hint_message(true, '', array('items' => array(array('id' => ' ', 'text' => '无'))));
        }

        $this->load->model('Shop_cat_model');
        $data = $this->Shop_cat_model->get_all_by_parent($parent_id);

        $item = get_list_data($data, 'Id', 'Name');

        if ($with_null !== '')
            $item[] = array('id' => ' ', 'text' => '无');

        hint_message(true, '', array('items' => $item));
    }

    private function get_brand_list_data($with_null)
    {
        $shop_id = $this->session->userdata('shop_id');
        $userid = $this->session->userdata('userid');

        $this->load->model('Brand_model');
        $all = $this->Brand_model->get_all($shop_id, $userid);

        return get_list_data($all, 'Id', 'Name', $with_null);
    }

    function get_brand()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $with_null = trim($this->input->get_post('with_null'));

        $data = $this->get_brand_list_data($with_null);

        hint_message(true, '', array('items' => $data));
    }

    public function get_unread_message_count()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return;

        $userid = $this->session->userdata('userid');

        $this->load->model('Messages_model');
        $message_count = $this->Messages_model->get_unread_count($userid);

        $this->load->model('Goods_consult_model');
        $consult_count = $this->Goods_consult_model->get_unread_count($userid);

        $desktop_notification = $this->session->userdata('desktop_notification');
        hint_message(true, '', array('desktop_notification' => $desktop_notification, 'message_count' => $message_count, 'consult_count' => $consult_count));
    }

    function get_province()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $this->load->model('Provinces_model');
        $all = $this->Provinces_model->get_all();

        $data = array();
        foreach ($all as $k => $v) {
            $data[] = array('id' => $v['Id'], 'text' => $v['Name']);
        }

        hint_message(true, '', array('items' => $data));
    }

    function get_city()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $province_id = trim($this->input->get_post('province_id'));
        if ($province_id === '')
            return hint_message(true, '', array('items' => array()));

        $this->load->model('Cities_model');
        $all = $this->Cities_model->get_all($province_id);

        $data = array();
        foreach ($all as $k => $v) {
            $data[] = array('id' => $v['Id'], 'text' => $v['Name']);
        }

        hint_message(true, '', array('items' => $data));
    }

    function get_district()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return hint_message(true, '', array('items' => array()));

        $city_id = trim($this->input->get_post('city_id'));
        if ($city_id === '')
            return hint_message(true, '', array('items' => array()));

        $this->load->model('Districts_model');
        $all = $this->Districts_model->get_all($city_id);

        $data = array();
        foreach ($all as $k => $v) {
            $data[] = array('id' => $v['Id'], 'text' => $v['Name']);
        }

        hint_message(true, '', array('items' => $data));
    }

    public function order_goods_data()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_ORDER))
            return datatable_data();

        $id = trim($this->input->get_post('id'));
        if($id == '')
            return datatable_data();

        $shop_id = $this->session->userdata('shop_id');

        $this->load->model('Order_model');
        $info = $this->Order_model->get_info_by_shop($shop_id, $id);
        if (! $info)
            return datatable_data();

        $list = unserialize($info['GoodsData']);

        foreach ($list as $k => $v) {
            $list[$k]['PromotionPrice'] = sprintf('%.2f', $v['PromotionPrice']);
        }

        return datatable_data(count($list), $list);
    }

    // 首页
	public function index()
	{
        if (! is_shopkeeper($this, 'Shopkeeper')) {
            redirect('.');
            return;
        }

        $menu = trim($this->input->get_post('menu'));

        $this->load->model('Userinfo_model');
        $info = $this->Userinfo_model->get_info($this->session->userdata('userid'));

        if ($menu === '')
            $menu = 'show_dashboard';

        $args = array(
            'userid' => $this->session->userdata('userid'),
            'shop_id' => $this->session->userdata('shop_id'),
            'name' => $this->session->userdata('name'),
            'shop_username' => $this->session->userdata('shop_username'),
            'shop_userid' => $this->session->userdata('shop_userid'),
            'platform_name' => get_platform_name($this),
            'account_permit' => intval($this->session->userdata('privilege')) & INVOICING_PRIVILEGE_BASIC,
            'info' => $info,
            'menu' => $menu,
        );

		$this->load->view('shopkeeper/main', $args);
	}

    // 监控中心
    function show_dashboard()
    {
        if (! is_shopkeeper($this, 'Shopkeeper'))
            return;

        $userid = $this->session->userdata('userid');
        $shop_id = $this->session->userdata('shop_id');

        $this->load->model('Userinfo_model');
        $userinfo_is_full = $this->Userinfo_model->info_is_full($userid);

        $this->load->model('Shops_model');
        $shop_info = $this->Shops_model->get_info_by_user($userid);

        $this->load->model('Shop_map_model');
        $shop_map_info = $this->Shop_map_model->get_info_by_user($userid);

        $this->load->model('User_vip_model');    
        $quota = $this->User_vip_model->get_gallery_quota($userid);
        $vip_info = $this->User_vip_model->get_shop_info($userid);

        $this->load->model('Gallery_model');    
        $used_space = $this->Gallery_model->get_used_space($userid);
        $gallery_all_space = get_readable_size($quota);
        $gallery_used_space = get_readable_size($used_space);

        $this->load->model('Goods_model');
        $on_sell_count = $this->Goods_model->get_on_sell_count($userid, array('filter' => ''));
        $product_upper_limit = $this->User_vip_model->get_product_upper_limit($userid);

        if ($shop_id) {
            $this->load->model('Order_model');
            $order_current_count = $this->Order_model->get_shop_current_count($shop_id, '', '', '', array('filter' => ''));
            $order_history_count = $this->Order_model->get_shop_history_count($shop_id, '', '', '', array('filter' => ''));
        } else {
            $order_current_count = 0;
            $order_history_count = 0;
        }

        $args = array(
            'shop_id' => $shop_id,
            'userinfo_is_full' => $userinfo_is_full,
            'shop_info' => $shop_info,
            'shop_map_info' => $shop_map_info,
            'vip_info' => $vip_info,
            'quota' => $quota,
            'used_space' => $used_space,
            'gallery_all_space' => $gallery_all_space,
            'gallery_used_space' => $gallery_used_space,
            'product_upper_limit' => $product_upper_limit,
            'on_sell_count' => $on_sell_count,
            'order_current_count' => $order_current_count,
            'order_history_count' => $order_history_count,
        );

		$this->load->view('shopkeeper/show_dashboard', $args);
    }

    // ...

    //热销商品管理
    public function show_hot_sale()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_GOODS))
            return;

        $shop_id = $this->session->userdata('shop_id');

        $this->load->model('Goods_model');
        $data = $this->Goods_model->get_list($shop_id);
        $goods_list = get_list_data($data, 'Id', 'Name', false);

        $args = array(
            'shop_id' => $shop_id,
            'goods_list' => $goods_list,
        );

        $this->load->view('shopkeeper/show_hot_sale', $args);
    }

    public function hot_sale_data()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_GOODS))
            return datatable_data();

        $args = datatable_args($this);

        $userid = $this->session->userdata('userid');

        $this->load->model('Hot_sale_model');
        $count = $this->Hot_sale_model->get_count($userid, $args);
        $list = $this->Hot_sale_model->get_list($userid, $args);

        return datatable_data($count, $list);
    }

    public function hot_sale_add()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_GOODS))
            return hint_message(false, '登录超时，请您重新登录！');

        try {
            $record = $this->input->get_post('record');

            $goods_id = trim($record['goods']);
            $sort = intval($record['sort']);

            if ($goods_id === '')
                return hint_message(false, '您必需指定商品！');
        } catch(Exception $e) {
            return hint_message(false, '价格不合法！');
        }

        $userid = $this->session->userdata('userid');
        $shop_id = $this->session->userdata('shop_id');

        $this->load->model('Hot_sale_model');
        $message = '';

        $this->db->trans_start();

        if ($this->Hot_sale_model->existed($userid, $goods_id))
            $message = '您指定的商品已经是热销商品！';

        if ($message === '' and ! $this->Hot_sale_model->add($userid, $shop_id, $goods_id, $sort))
            $message = '操作失败，请您重试';

        $this->db->trans_complete();

        if($message !== '')
            return hint_message(false, $message);

        hint_message();
    }

    public function hot_sale_update()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_GOODS))
            return hint_message(false, '登录超时，请您重新登录！');

        try {
            $record = $this->input->get_post('record');

            $id = trim($record['id']);
            $goods_id = trim($record['goods']);
            $sort = intval($record['sort']);

            if ($id === '')
                throw new Exception('a');

            if ($goods_id === '')
                return hint_message(false, '您必需指定商品！');
        } catch(Exception $e) {
            return hint_message(false, '价格不合法！');
        }

        $userid = $this->session->userdata('userid');
        $shop_id = $this->session->userdata('shop_id');

        $this->load->model('Hot_sale_model');
        $message = '';

        $this->db->trans_start();

        if ($this->Hot_sale_model->existed($userid, $goods_id, $id))
            $message = '您指定的商品已经是热销商品！';

        if ($message === '' and ! $this->Hot_sale_model->update($id, $userid, $goods_id, $sort))
            $message = '操作失败，请您重试';

        $this->db->trans_complete();

        if($message !== '')
            return hint_message(false, $message);

        hint_message();
    }

    public function hot_sale_remove()
    {
        if (! is_shopkeeper($this, 'Shopkeeper', SHOP_PRIVILEGE_GOODS))
            return hint_message(false, '登录超时，请您重新登录！');

        $id = trim($this->input->get_post('id'));
        if($id === '')
            return hint_message(false, 'ID不能为空！');

        $userid = $this->session->userdata('userid');

        $this->load->model('Hot_sale_model');
        if (! $this->Hot_sale_model->remove($userid, $id))
            return hint_message(false, '删除失败，请您重试！');

        hint_message();
    }

}
